package cn.powerone.scm.upload.controller;

import java.util.List;

import cn.powerone.common.core.domain.entity.SysUser;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.powerone.common.annotation.Log;
import cn.powerone.common.core.controller.BaseController;
import cn.powerone.common.core.domain.AjaxResult;
import cn.powerone.common.enums.BusinessType;
import cn.powerone.scm.upload.domain.ScmImportInquiriesIn;
import cn.powerone.scm.upload.service.IScmImportInquiriesInService;
import cn.powerone.common.utils.poi.ExcelUtil;
import cn.powerone.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 导入询价BatchController
 * 
 * @author PowerOne
 * @date 2025-07-07
 */
@RestController
@RequestMapping("/scm/scmImportInquiriesIn")
public class ScmImportInquiriesInController extends BaseController
{
    @Autowired
    private IScmImportInquiriesInService scmImportInquiriesInService;

    /**
     * 查询导入询价Batch列表
     */
    @PreAuthorize("@ss.hasPermi('scm:scmImportInquiriesIn:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScmImportInquiriesIn scmImportInquiriesIn)
    {
        startPage();
        List<ScmImportInquiriesIn> list = scmImportInquiriesInService.selectScmImportInquiriesInList(scmImportInquiriesIn);
        return getDataTable(list);
    }

    /**
     * 导出导入询价Batch列表
     */
    @PreAuthorize("@ss.hasPermi('scm:scmImportInquiriesIn:export')")
    @Log(title = "导入询价Batch", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScmImportInquiriesIn scmImportInquiriesIn)
    {
        List<ScmImportInquiriesIn> list = scmImportInquiriesInService.selectScmImportInquiriesInList(scmImportInquiriesIn);
        ExcelUtil<ScmImportInquiriesIn> util = new ExcelUtil<ScmImportInquiriesIn>(ScmImportInquiriesIn.class);
        util.exportExcel(response, list, "导入询价Batch数据");
    }

    /**
     * 获取导入询价Batch详细信息
     */
    @PreAuthorize("@ss.hasPermi('scm:scmImportInquiriesIn:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(scmImportInquiriesInService.selectScmImportInquiriesInById(id));
    }

    /**
     * 新增导入询价Batch
     */
    @PreAuthorize("@ss.hasPermi('scm:scmImportInquiriesIn:add')")
    @Log(title = "导入询价Batch", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScmImportInquiriesIn scmImportInquiriesIn)
    {
        return toAjax(scmImportInquiriesInService.insertScmImportInquiriesIn(scmImportInquiriesIn));
    }

    /**
     * 修改导入询价Batch
     */
    @PreAuthorize("@ss.hasPermi('scm:scmImportInquiriesIn:edit')")
    @Log(title = "导入询价Batch", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScmImportInquiriesIn scmImportInquiriesIn)
    {
        return toAjax(scmImportInquiriesInService.updateScmImportInquiriesIn(scmImportInquiriesIn));
    }

    /**
     * 删除导入询价Batch
     */
    @PreAuthorize("@ss.hasPermi('scm:scmImportInquiriesIn:remove')")
    @Log(title = "导入询价Batch", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(scmImportInquiriesInService.deleteScmImportInquiriesInByIds(ids));
    }

    /**
     * 导入数据
     */
    @Log(title = "询价导入", businessType = BusinessType.IMPORT)
    // @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport, Long projectId) throws Exception
    {
        ExcelUtil<ScmImportInquiriesIn> util = new ExcelUtil<ScmImportInquiriesIn>(ScmImportInquiriesIn.class);
        List<ScmImportInquiriesIn> scmImportInquiriesInList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = scmImportInquiriesInService.importData(scmImportInquiriesInList, updateSupport, operName,projectId);
        // 异步调用处理数据
        scmImportInquiriesInService.processImportData();
        return success(message);
    }


    /**
     * 导入模板下载
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<ScmImportInquiriesIn> util = new ExcelUtil<ScmImportInquiriesIn>(ScmImportInquiriesIn.class);
        util.importTemplateExcel(response, "询价数据");
    }
}
